<?php

namespace App\Containers\AppSection\Project\Actions;

use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Project\Models\Project;
use App\Containers\AppSection\Project\Tasks\CreateAnalyticTask;
use App\Containers\AppSection\Project\Tasks\CreateCompanyTask;
use App\Containers\AppSection\Project\Tasks\CreateConditionTask;
use App\Containers\AppSection\Project\Tasks\CreateDocumentTask;
use App\Containers\AppSection\Project\Tasks\CreateGovernmentTask;
use App\Containers\AppSection\Project\Tasks\CreateProjectTask;
use App\Containers\AppSection\Settings\Tasks\FindSettingByKeyTask;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;

class CreateProjectAction extends Action
{
    public function run(Request $request): Project
    {
        $company = app(CreateCompanyTask::class)->run([]);
        $condition = app(CreateConditionTask::class)->run([
            'type_key' => 'purchase',
            'rating_key' => 'b',
            'repayment_key' => 'annuity',
            'output_key' => 'automatically'
        ]);
        $rate = app(FindSettingByKeyTask::class)->run('project_rate');

        $entries_egrul = app(FindSettingByKeyTask::class)->run('project_analytics_entries_egrul');
        $arbitration_cases = app(FindSettingByKeyTask::class)->run('project_analytics_arbitration_cases');
        $government_purchases = app(FindSettingByKeyTask::class)->run('project_analytics_government_purchases');
        $analytic = app(CreateAnalyticTask::class)->run([
            'entries_egrul' => [$entries_egrul->value],
            'arbitration_cases' => $arbitration_cases->value,
            'government_purchases' => $government_purchases->value,
        ]);

        $governmentId = null;
        if ($request->profile()->hasLegalForm('entity')) {
            $participants = app(FindSettingByKeyTask::class)->run('project_governments_participants');
            $persons_information = app(FindSettingByKeyTask::class)->run('project_governments_persons_information');
            $government = app(CreateGovernmentTask::class)->run([
                'participants' => [$participants->value],
                'persons_information' => [$persons_information->value],
                'type_key' => 'collegial_governing_body'
            ]);
            $governmentId = $government->id;
        }


        $businessModels = app(FindSettingByKeyTask::class)->run('project_businesses');
        $project = app(CreateProjectTask::class)->run([
            'profile_id' => $request->profile()->id,
            'company_id' => $company->id,
            'condition_id' => $condition->id,
            'businesses' => $businessModels->value,
            'analytic_id' => $analytic->id,
            'government_id' => $governmentId,
            'rate' => $rate->value,
            'stage_key' => 'seed',
            'moderation_status_key' => 'projects_not_moderated',
            'is_draft' => true,
            'project_rating_key' => 'b',
            'profile_rating_key' => 'b',
            'document_rating_key' => 'b',
        ]);


        $documents = $request->profile()->hasLegalForm('ie') ?
            app(FindSettingByKeyTask::class)->run('project_documents_ie') :
            app(FindSettingByKeyTask::class)->run('project_documents_entity');

        foreach ($documents->value as $document) {
            app(CreateDocumentTask::class)->run([
                'project_id' => $project->id,
                'label' => $document['label'],
                'files' => $document['files'],
                'required' => $document['required'],
            ]);
        }


        app(CreateLogTask::class)->run($request->profile()->id, "Проект №{$project->id} создан", 'project');

        return $project;
    }
}
